package cn.damai.controller.web;

import cn.damai.common.*;
import cn.damai.model.User;
import cn.damai.search.service.TestService;
import cn.damai.search.vo.*;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import cn.damai.search.service.ItemSearchService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索控制层
 */
@Controller
@RequestMapping("search")
public class SearchController {

    @Reference(url = DubboUtil.SEARCH_URL,version = DubboUtil.VRESION ,group = DubboUtil.GROUP)
    private TestService testService;
    //搜索服务
    @Reference(url = DubboUtil.SEARCH_URL,version = DubboUtil.VRESION ,group = DubboUtil.GROUP)
    private ItemSearchService itemSearchService;
    /**
     * DtoUtil方法 是 封装成json格式数据   前端需要什么格式的数据 可以修改此方法
     * HotItemVo  返回给前端的数据格式
     *  测试api工具  Postman
     */
    // 测试方法1 首页轮播图api
    @RequestMapping(value = "banner",method = RequestMethod.POST)
    @ResponseBody
    public Dto<HotItemVo> queryBanner(){
        List<HotItemVo> hotItemVos = testService.queryBanner();
        return DtoUtil.returnDataSuccess(hotItemVos);
    }

    //测试方法2 商品详情api
    @RequestMapping(value = "item",method = RequestMethod.POST)
    @ResponseBody
    public Dto<ItemDetailVo> queryItemDatail(@RequestBody Map<String,Object> param){
        if (EmptyUtils.isEmpty(param)) {
            return DtoUtil.returnFail("失败","0101");
        }else {
            Integer id = Integer.parseInt(param.get("id").toString());
            Dto<ItemDetailVo> itemDetailVoDto = testService.queryItemDetail((long) id);
            return itemDetailVoDto;
        }
    }

    //测试方法3 - 商品详情 排期api
    @RequestMapping(value = "schedule",method = RequestMethod.POST)
    @ResponseBody
    public Dto<List<ItemSchedulerVo>> queryItemScheduler(@RequestBody Map<String,Object> param){
        Integer id = Integer.parseInt(param.get("itemId").toString());
        return testService.queryItemScheduler((long)id);
    }

    //测试方法4 - 商品排期 查询价格api
    @RequestMapping(value = "price",method = RequestMethod.POST)
    @ResponseBody
    public Dto<List<ItemPriceVo>> queryitemPrice(@RequestBody Map<String,Object> param){
        Integer id = Integer.parseInt(param.get("scheduleId").toString());
        return testService.queryItemPrice((long)id);
    }

    //测试方法5 - 账号密码登陆api  密码MD5加密  测试账号:sdodh366@163.com 密码:000000 ,ok,功能算完整!最后一个 方法需要修改
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public Dto loginByPassword(@RequestBody User user){
        user.setPassword(MD5.getMd5(user.getPassword(),32));
        Object[] results = testService.login(user);
        if (EmptyUtils.isEmpty(results)) {
            return DtoUtil.returnFail("登陆失败","0000");
        }else{
            return DtoUtil.returnSuccess("登陆成功",results);
        }
    }

    ///=====================================================================


    //搜索首页-基本数据
    @RequestMapping("/")
    public  String home(Model model){
        //区域参数
     //   List<DmAreaVo> areaVoList = itemSearchService.queryArea();
        // 商品分类参数
      //  List<DmItemTypeVo> itemTypeVoList = itemSearchService.queryItemType("0");
        //model.addAttribute("areaList",areaVoList);
        //model.addAttribute("itemType1",itemTypeVoList);
        return "search";
    }

//    其他页面搜索内容跳转
    @RequestMapping("content")
    public String content(@RequestParam("searchName")String searchName,Model model){
      //  List<DmAreaVo> areaVoList = itemSearchService.queryArea();
        // 商品分类参数
        //List<DmItemTypeVo> itemTypeVoList = itemSearchService.queryItemType("0");
        //model.addAttribute("areaList",areaVoList);
        //model.addAttribute("itemType1",itemTypeVoList);
        model.addAttribute("searchName",searchName);
        return "search";
    }

    //    其他页面搜索类型跳转
    @RequestMapping("type")
    public String type(@RequestParam("searchType")Integer searchType,Model model){
//        List<DmAreaVo> areaVoList = itemSearchService.queryArea();
//        // 商品分类参数
//        List<DmItemTypeVo> itemTypeVoList = itemSearchService.queryItemType("0");
//        model.addAttribute("areaList",areaVoList);
//        model.addAttribute("itemType1",itemTypeVoList);
        model.addAttribute("searchType",searchType);
        return "search";
    }

    // 商品子类
    @RequestMapping("/queryItemType")
    @ResponseBody
    public List<DmItemTypeVo> itemtype(@RequestParam(value = "itemType1",required=false)String itemType1){
        //通过分类id 数数子类
        Integer item2Num = itemSearchService.queryItemCount(itemType1);
        if(Integer.parseInt(itemType1) !=0  && item2Num !=0){
                return itemSearchService.queryItemType(itemType1);
        }
        return null;
    }

    //搜索主方法
    @RequestMapping(value = "test",method = RequestMethod.POST)
    @ResponseBody
    public Page<ItemSearchVo> objects(@RequestParam(value = "itemType1",required=false)String itemType1,
                                      @RequestParam(value = "area",required=false)String area,
                                      @RequestParam(value = "itemType2",required=false)String itemType2,
                                      @RequestParam(value = "curPage",required=false)String curPage,
                                      @RequestParam(value = "text",required=false)String text){
        Map<String,Object> param =new HashMap<>();
        //判断值是否为空
        if(EmptyUtils.isEmpty(text)){text="";}
        if (EmptyUtils.isEmpty(area)){area = "0";}
        if(EmptyUtils.isEmpty(itemType1)){itemType1 = "0";}
        if(EmptyUtils.isEmpty(itemType2)){itemType2 = "0";}
        if(EmptyUtils.isEmpty(curPage)){curPage="1";}
        //当前页数
        if(Integer.parseInt(curPage) <= 0){curPage="1";}
        //1城市 2分类 3子类
        if(Integer.parseInt(area)!=0){param.put("areaId",area);}
        if(Integer.parseInt(itemType1)!=0){param.put("itemTypeId1",itemType1);}
        if(Integer.parseInt(itemType2)!=0){param.put("itemTypeId2",itemType2); }
        if(text != ""){param.put("text",text);}

        Page<ItemSearchVo> page = itemSearchService.queryItemList(Integer.parseInt(curPage),param);
        System.out.println("参数个数:"+param.size()+" 数据数量:"+page.getRows().size() +" 总页数:"+ page.getCurPage());

        return page;

    }


    //模糊查询 - 动态商品 城市 分类
    @RequestMapping("/queryItemType2")
    @ResponseBody
    public SearchParametersVo itemtype4(@RequestParam(value = "itemType1",required=false)String itemType1,
                                        @RequestParam(value = "itemType2",required=false)String itemType2,
                                        @RequestParam(value = "area",required=false)String area,
                                        @RequestParam(value = "text",required=false)String text) {
        //判断值是否为空
        if(EmptyUtils.isEmpty(text)){text="";}
        if (EmptyUtils.isEmpty(area)){area = "0";}
        if(EmptyUtils.isEmpty(itemType1)){itemType1 = "0";}
        if(EmptyUtils.isEmpty(itemType2)){itemType2 = "0";}

        Map<String, Object> params = new HashMap<>();
        if (Integer.parseInt(area)!=0){params.put("areaId", area);}
        if (text != "" && text != null) { params.put("text", text); }
        if (Integer.parseInt(itemType1)!=0) { params.put("itemTypeId1", itemType1); }
        if(Integer.parseInt(itemType2)!=0){params.put("itemTypeId2",itemType2); }
        // System.out.println(area +" " + itemType1 +" "+ itemType2 + " :" + text);
        SearchParametersVo searchParametersVo = itemSearchService.queryGrouping(params);

        return searchParametersVo;
    }

    //模糊查询 - 动态商品 子类
    @RequestMapping("/queryItemType1")
    @ResponseBody
    public  List<DmItemTypeVo> itemtype3(@RequestParam(value = "itemType1",required=false)String itemType1,
                                         @RequestParam(value = "itemType2",required=false)String itemType2,
                                         @RequestParam(value = "area",required=false)String area,
                                         @RequestParam(value = "text",required=false)String text){
        //判断值是否为空
        if(EmptyUtils.isEmpty(text)){text="";}
        if (EmptyUtils.isEmpty(area)){area = "0";}
        if(EmptyUtils.isEmpty(itemType1)){itemType1 = "0";}
        if(EmptyUtils.isEmpty(itemType2)){itemType2 = "0";}

        Map<String, Object> params = new HashMap<>();
        if (Integer.parseInt(area)!=0){params.put("areaId", area);}
        if (text != "" && text != null) { params.put("text", text); }
        if (Integer.parseInt(itemType1)!=0) { params.put("itemTypeId1", itemType1); }
        if(Integer.parseInt(itemType2)!=0){params.put("itemTypeId2",itemType2); }
        // System.out.println(area +" " + itemType1 +" "+ itemType2 + " :" + text);
        List<DmItemTypeVo> itemType2List = itemSearchService.queryItemTypeGrouping1(params);
        return itemType2List ;
    }

    //普通查询-固定商品参数 城市,分类 ok
    @RequestMapping("/test2")
    @ResponseBody
    public  SearchParametersVo itemtype2(){
        List<DmAreaVo> areaVoList = itemSearchService.queryArea();
        List<DmItemTypeVo> itemTypeVoList = itemSearchService.queryItemType("0");
        SearchParametersVo searchParametersVo = new SearchParametersVo();
        searchParametersVo.setAreaVos(areaVoList);
        searchParametersVo.setItemType1Vos(itemTypeVoList);
        return searchParametersVo;
    }




}


